package com.murphy.algorithm.likou;

import com.alibaba.fastjson.JSON;

/**
 * @author dongsufeng
 * @version 4.0
 * @date 2023/2/20 15:54
 */
public class addTwoNumbers {
    /**
     * 给你两个 非空 的链表，表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的，并且每个节点只能存储 一位 数字。
     *
     * 请你将两个数相加，并以相同形式返回一个表示和的链表。
     *
     * 你可以假设除了数字 0 之外，这两个数都不会以 0 开头
     *
     * 来源：力扣（LeetCode）
     * 链接：https://leetcode.cn/problems/add-two-numbers
     * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
     * @param l1
     * @param l2
     * @return
     */
    public ListNode addTwoNumbers1(ListNode l1, ListNode l2) {
        ListNode newNode = null, n2 = null;
        int j = 0;
        while (l1!=null || l2!=null){
            int l1v=l1==null ? 0 : l1.val;
            int l2v=l2==null ? 0 : l2.val;
            int val = l1v+l2v+j;
            j = val>=10 ? 1 : 0;
            if (newNode==null) {
                newNode = n2 = new ListNode(val % 10);
            }else {
                n2.next = new ListNode(val % 10);
                n2=n2.next;
            }
            l1 = l1!=null ? l1.next : null;
            l2 = l2!=null ? l2.next : null;
        }
        if (j>0){
            n2.next=new ListNode(j);
        }
        return newNode;
    }

    /**
     * 两个数相加
     * @param l1
     * @param l2
     * @return
     */
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode listNode = null;
        ListNode n2 = null;
        int j = 0;
        while (l1 != null || l2 != null){
            int temp = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val);
            int v = temp + j;
            j = v >= 10 ? 1 : 0;
            if (listNode == null){
                listNode = new ListNode(v % 10);
                n2 = listNode;
            }else {
                n2.next = new ListNode(v % 10);
                n2 = n2.next;
            }
            if (l1 != null) {
                l1 = l1.next;
            }
            if (l2 != null) {
                l2 = l2.next;
            }
        }
        if (j > 0){
            n2.next = new ListNode(j);
        }

        return listNode;
    }
    public static void main(String[] args) {
        ListNode l1 = new ListNode(9);
        l1.next = new ListNode(9);
        l1.next.next=new ListNode(9);
        l1.next.next.next=new ListNode(9);

        ListNode l2 = new ListNode(9);
        l2.next = new ListNode(9);
//        l2.next.next=new ListNode(4);
        addTwoNumbers addTwoNumbers = new addTwoNumbers();
        ListNode newnode = addTwoNumbers.addTwoNumbers(l1,l2);

        while (newnode!=null){
            System.out.print(newnode.val);
            newnode = newnode.next;
        }
        System.out.println("=========");
        ListNode newnode1 = addTwoNumbers.addTwoNumbers(l1,l2);

        while (newnode1!=null){
            System.out.print(newnode1.val);
            newnode1 = newnode1.next;
        }

    }

    public static class ListNode{
        int val;
        ListNode next;
        public ListNode() {}
        public ListNode(int val) { this.val = val; }
        public ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }

}
